package com.filldream.fastboot.common.entity;

import lombok.Data;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Collections;
import java.util.List;

/**
 * 分页列表返回类
 * @author RickSun
 * @see 3.0
 * 2019-03-22 15:56
 */
@Data
public class P<T> extends B {
	
	/** 总记录  */
	private long total;
	/** 记录列表  */
	private List<T> data;
	/** 总页数  */
	private long pages;

	/**
	 * 第几页
	 */
	private long pageNo;

	private long pageSize;


	/**
	 * 空参构造方法
	 */
	public P(){}

	/**
	 * 整型构造方法
	 * @param code
	 * @param total
	 * @param data
	 * @param msg
	 * @param pageNo
	 * @param pageSize
	 */
	public P(int code, Long total, List<T> data, String msg, String enmsg, Integer pageNo, Integer pageSize){
		this.code = code;
		this.total = total;
		this.pageNo = pageNo.longValue();
		this.pageSize = pageSize.longValue();
		this.data = data;
		this.msg = msg;
		this.enmsg = enmsg;
		this.pages = (long) Math.ceil((double) this.total / this.pageSize);;
	}

	/**
	 * 默认整型成功
	 * @param total	总条数
	 * @param data	list内容
	 * @param pageNo	当前页
	 * @param pageSize	每页的数量
	 * @return
	 */
	public static <T> P<T> success(long total, List<T> data, Integer pageNo, Integer pageSize){
		ErrorCode success = ErrorCode.SUCCESS;
		return new P(success.getCode(),total,data,success.getMsg(),success.getEnMsg(),pageNo,pageSize);
	}

	public static <T> P<T> success(long total, List<T> data, BasePage basePage){
		ErrorCode success = ErrorCode.SUCCESS;
		return new P(success.getCode(),total,data,success.getMsg(),success.getEnMsg(),basePage.getPageNo(),basePage.getPageSize());
	}

	public static <T> P<T> autoSuccess(long total, List<T> data, BasePage basePage){
		ErrorCode success = ErrorCode.SUCCESS;
		if(data.isEmpty()){
			return empty(total,basePage);
		}
		return new P(success.getCode(),total,data,success.getMsg(),success.getEnMsg(),basePage.getPageNo(),basePage.getPageSize());
	}

	public static <T> P<T> autoSuccess(Page page){
		ErrorCode success = ErrorCode.SUCCESS;
		if( page.getRecords() == null || page.getRecords().isEmpty()  ){
			success = ErrorCode.EMPTY_RESULT;
		}
		return new P(success.getCode(),Long.valueOf(page.getTotal()),page.getRecords(),success.getMsg(),success.getEnMsg(),Integer.valueOf(page.getCurrent()+""),Integer.valueOf(page.getSize()+""));
	}

	public static <T> P<T> autoSuccess(List<T> data,Page page){
		page.setRecords( data );
		return autoSuccess(page);
	}


	public static P empty(Long total , BasePage basePage){
		ErrorCode success = ErrorCode.EMPTY_RESULT;
		return new P(success.getCode(),total,Collections.emptyList(),"empty","empty",basePage.getPageNo(),basePage.getPageSize());
	}

	/**
	 * 默认失败
	 * @return
	 */
	public static P fail(BasePage basePage){
		ErrorCode fail = ErrorCode.FAIL;
		return new P(fail.getCode(),
				0L,Collections.emptyList(),fail.getMsg(),fail.getEnMsg(),
				basePage.getPageNo(),basePage.getPageSize());
	}

	/**
	 * 默认失败
	 * @return
	 */
	public static P fail(ErrContent errContent , BasePage basePage){
		return new P(errContent.getCode(),
				0L,Collections.emptyList(),errContent.getMsg(),errContent.getEnMsg(),
				basePage.getPageNo(),basePage.getPageSize());
	}

	@Override
	public P put(String key, Object value) {
		checkMap();
		this.otherMap.put(key,value);
		return this;
	}

}
